package com.ruoyi.generator.service.impl;

import com.ruoyi.generator.entity.*;
import com.ruoyi.generator.entity.bo.PlanAssociationBO;
import com.ruoyi.generator.entity.bo.PlanScheduleBO;
import com.ruoyi.generator.mapper.*;
import com.ruoyi.generator.service.CheckPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Service
public class CheckPlanServiceImpl implements CheckPlanService {

    @Autowired
    private CheckPlanMapper checkPlanMapper;

    @Autowired
    private PlanAssociationMapper planAssociationMapper;

    @Autowired
    private ShiftDefaultMapper shiftDefaultMapper;

    @Autowired
    private PlanScheduleMapper planScheduleMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 获取点检计划列表
     * @return
     */
    @Override
    public List<CheckPlan> getCheckPlanList(CheckPlan checkPlan) {
        List<CheckPlan> list = checkPlanMapper.selectCheckPlanList(checkPlan);
        return list;
    }

    /**
     * 获取设备所有信息
     * @return
     */
    @Override
    public List<PlanAssociationBO> getPlanAssociationInfo() {

        List<PlanAssociationBO> planAssociationBOS = planAssociationMapper.selectPlanAssociationInfo();
        return planAssociationBOS;
    }


    /**
     * 获取设备所有列表
     * @param planAssociation
     * @return
     */
    @Override
    public List<PlanAssociationBO> getPlanAssociationList(PlanAssociation planAssociation) {
        List<PlanAssociationBO> planAssociationBOList = planAssociationMapper.selectPlanAssociationList(planAssociation);
        return planAssociationBOList;
    }


    /**
     * 新增点检周期
     * @param planScheduleBO
     * @return
     */
    @Transactional
    @Override
    public int addPlanAssociation(PlanScheduleBO planScheduleBO) {
        int rows = 0;
        for (Integer planAssociationId : planScheduleBO.getPlanAssociationId()) {
            Schedule schedule = scheduleMapper.selectScheduleById(planScheduleBO.getScheduleId());
            rows = planAssociationMapper.updatePlanAssociation(planAssociationId,planScheduleBO.getScheduleId(),planScheduleBO.getPlanName());
            List<ShiftDefault> shiftDefaults = shiftDefaultMapper.selectShiftDefaultList(planScheduleBO.getScheduleId());
            for (ShiftDefault shiftDefault : shiftDefaults) {
                if (shiftDefault.getTime().contains("当日")){
                    //获取当前时间
                    LocalDateTime now = LocalDateTime.now();
                    // 定义时间格式
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
                    // 格式化当前时间
                    String formattedTime = now.format(formatter);
                    // 字符串转LocalDateTime
                    LocalTime timem = LocalTime.parse(formattedTime, formatter);
                    // 提取分钟值,当前时间的分钟值
                    Integer minutes = timem.getHour() * 60 + timem.getMinute();

                    //结束时间
                    LocalTime editTime = LocalTime.parse(shiftDefault.getTime().substring(shiftDefault.getTime().length() - 5),formatter);
                    //开始时间
                    LocalTime endTime = LocalTime.parse(shiftDefault.getTime().substring(2, 7),formatter);
                    // 字符串转LocalDateTime
                    // 提取分钟值
                    Integer editMinutes = editTime.getHour()*60+editTime.getMinute();
                    Integer endMinutes = endTime.getHour()*60+endTime.getMinute();
                    //开始时间对比当前时间
                    if(minutes < endMinutes){
                        PlanSchedule planSchedule = new PlanSchedule();
                        planSchedule.setPlanAssociationId(planAssociationId);
                        planSchedule.setScheduleId(planScheduleBO.getScheduleId());
                        planSchedule.setShiftDefaultId(shiftDefault.getId());
                        planSchedule.setPeriod(schedule.getPeriod());
                        planSchedule.setCheckStatus("未开始");
                        planScheduleMapper.insertPlanSchedule(planSchedule);
                    }else if (minutes > endMinutes && minutes < editMinutes){
                        PlanSchedule planSchedule = new PlanSchedule();
                        planSchedule.setPlanAssociationId(planAssociationId);
                        planSchedule.setScheduleId(planScheduleBO.getScheduleId());
                        planSchedule.setShiftDefaultId(shiftDefault.getId());
                        planSchedule.setPeriod(schedule.getPeriod());
                        planSchedule.setCheckStatus("运行中");
                        planScheduleMapper.insertPlanSchedule(planSchedule);
                    }else if (minutes > editMinutes){
                        PlanSchedule planSchedule = new PlanSchedule();
                        planSchedule.setPlanAssociationId(planAssociationId);
                        planSchedule.setScheduleId(planScheduleBO.getScheduleId());
                        planSchedule.setShiftDefaultId(shiftDefault.getId());
                        planSchedule.setPeriod(schedule.getPeriod());
                        planSchedule.setCheckStatus("未开始");
                        planScheduleMapper.insertPlanSchedule(planSchedule);
                    }
                } else {
                    PlanSchedule planSchedule = new PlanSchedule();
                    planSchedule.setPlanAssociationId(planAssociationId);
                    planSchedule.setScheduleId(planScheduleBO.getScheduleId());
                    planSchedule.setShiftDefaultId(shiftDefault.getId());
                    planSchedule.setPeriod(schedule.getPeriod());
                    planSchedule.setCheckStatus("未开始");
                    planScheduleMapper.insertPlanSchedule(planSchedule);
                }
            }
        }
        return rows;
    }
}
